#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    rendererWidget = new RendererWidget(this);
    ui->RendererWidgetLayout->addWidget(rendererWidget);
    connect(ui->loadCSG_bt , SIGNAL(clicked()) , SLOT(chooseCSGFile()));
    connect(ui->resetCam_bt , SIGNAL(clicked()) , SLOT(resetCam()));
    connect(ui->isosurface_bt , SIGNAL(clicked()) , SLOT(calcIsosurface()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::chooseCSGFile(){
    QString fileName;

    fileName = QFileDialog::getOpenFileName(this, "Select the CSG file", "../", "CSG files (*.csg)");
    if(!fileName.isNull()){
        this->rendererWidget->readCSGFile(fileName);
        calcIsosurface();
    }

    this->rendererWidget->updateGL();
}

void MainWindow::resetCam(){
    this->rendererWidget->resetCam();
}

void MainWindow::calcIsosurface(){
    int aiDims[3];
    double adSpacing[3];
    XYZ origin;

    aiDims[0] = ui->dimX_spin->value();         aiDims[1] = ui->dimY_spin->value();         aiDims[2] = ui->dimZ_spin->value();
    adSpacing[0] = ui->spaceX_spin->value();    adSpacing[1] = ui->spaceY_spin->value();    adSpacing[2] = ui->spaceZ_spin->value();
    origin.x = ui->originX_spin->value();       origin.y = ui->originY_spin->value();       origin.z = ui->originZ_spin->value();

    int numPolygons = this->rendererWidget->calcIsoSurface(origin , aiDims , adSpacing);
    stringstream ss;
    ss << numPolygons;
    ui->numPoligons_txt->setText(ss.str().c_str());
}
